﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ATM
{
    public partial class ATM : Form
    {
        public bool powerOn;
        public Decimal moneyAmount;
        public String commandString;
        public int commandNumber;
        public BankDatabase bankDatabase;
        public String currentAccountID;
        public Receipt receipt;
        public Withdrawal withdrawal;
        public TransferAccounts transferAccounts;
        public Deposit deposit;
        public BalanceInquiry balanceInquiry;

        /****ATM机可能处于的状态******/
        public ATMState powerOffState;
        public ATMState powerOnState;
        public ATMState checkPasswordState;
        public ATMState chooseTransactionState;
        public ATMState balanceInquiryState;
        public ATMState printReceiptState;
        public ATMState inputWithdrawAmountState;
        public ATMState takeMoneyState;
        public ATMState transferAccountState1;
        public ATMState transferAccountState2;
        public ATMState transferAccountState3;
        public ATMState insertEnvelopState;
        public ATMState depositState;

        public Log log;
        /*****************************/

        /****ATM机当前状态************/
        public ATMState state;

        /*****************************/
        public String belongTo;       //ATM机属于哪家银行,A或者B


        public ATM()
        {
            InitializeComponent();

            /********初始化各状态*********/
            powerOffState = new PowerOffState(this);
            powerOnState = new PowerOnState(this);
            checkPasswordState = new CheckPasswordState(this);
            chooseTransactionState = new ChooseTransactionState(this);
            balanceInquiryState = new BalanceInquiryState(this);
            printReceiptState = new PrintReceiptState(this);
            inputWithdrawAmountState = new InputWithdrawAmountState(this);
            takeMoneyState = new TakeMoneyState(this);
            transferAccountState1 = new TransferAccountsState1(this);
            transferAccountState2 = new TransferAccountsState2(this);
            transferAccountState3 = new TransferAccountsState3(this);
            insertEnvelopState = new InsertEnvelopState(this);
            depositState = new DepositState(this);
            /*****************************/

            this.belongTo = "B";
            this.bankDatabase = new BankDatabase();
            this.deposit = new Deposit();
            this.transferAccounts = new TransferAccounts();
            this.balanceInquiry = new BalanceInquiry();
            this.receipt = new Receipt("");
            this.withdrawal = new Withdrawal();
            this.log = new Log();
            this.state = powerOffState;
            this.moneyAmount = 10000;
            this.commandString = "";
            this.commandNumber = 0;
            this.keypadScreen.Text = "";
            this.powerOn = false;
            this.switchButton.Text = "Power On";
            this.screen.BackColor = Color.LightGray;
            this.keypadScreen.BackColor = Color.LightGray;

            this.oneButton.Enabled = false;
            this.twoButton.Enabled = false;
            this.threeButton.Enabled = false;
            this.fourButton.Enabled = false;
            this.fiveButton.Enabled = false;
            this.sixButton.Enabled = false;
            this.sevenButton.Enabled = false;
            this.eightButton.Enabled = false;
            this.nineButton.Enabled = false;
            this.zeroButton.Enabled = false;
            this.enterButton.Enabled = false;
            this.insertEnvelopButton.Enabled = false;
            this.takeCashButton.Enabled = false;
        }

        public void setState(ATMState currentState)
        {
            this.state = currentState;
        }

        private void switchButton_Click(object sender, EventArgs e)
        {
            state.pressPowerButton();
        }

        private void oneButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "1";
        }

        private void twoButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "2";
        }

        private void threeButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "3";
        }

        private void fourButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "4";
        }

        private void fiveButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "5";
        }

        private void sixButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "6";
        }

        private void sevenButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "7";
        }

        private void eightButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "8";
        }

        private void nineButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "9";
        }

        private void zeroButton_Click(object sender, EventArgs e)
        {
            this.keypadScreen.Text += "0";
        }

        private void enterButton_Click(object sender, EventArgs e)
        {
            this.state.pressEnterButton();
        }

        private void takeCashButton_Click(object sender, EventArgs e)
        {
            this.state.pressTakeCashButton();
        }

        private void insertEnvelopButton_Click(object sender, EventArgs e)
        {
            this.state.pressInsertEnvelopButton();
        }

        private void enterManager_Click(object sender, EventArgs e)
        {
            Manager manager = new Manager(this);
            manager.Show();
        }  
    }
}
